package edu.nwpu.zya.coreops.modules.resource.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aventrix.jnanoid.jnanoid.NanoIdUtils;
import edu.nwpu.zya.coreops.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import edu.nwpu.zya.coreops.modules.resource.entity.EcsEntity;
import edu.nwpu.zya.coreops.modules.resource.service.EcsService;
import edu.nwpu.zya.coreops.common.utils.PageUtils;
import edu.nwpu.zya.coreops.common.utils.R;



/**
 * 
 *
 * @author zhouyuang
 * @email 742025491@qq.com
 * @date 2022-04-18 22:23:40
 */
@RestController
@RequestMapping("resource/ecs")
public class EcsController {
    @Autowired
    private EcsService ecsService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("resource:ecs:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("userId",ShiroUtils.getUserId());
        PageUtils page = ecsService.queryEcsVoPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("resource:ecs:info")
    public R info(@PathVariable("id") Long id){
		EcsEntity ecs = ecsService.getById(id);

        return R.ok().put("ecs", ecs);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("resource:ecs:save")
    public R save(@RequestBody EcsEntity ecs){
        ecs.setUserId(ShiroUtils.getUserId());
        ecs.setEcsId(NanoIdUtils.randomNanoId());
		ecsService.saveEcs(ecs);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("resource:ecs:update")
    public R update(@RequestBody EcsEntity ecs){
		ecsService.updateByRds(ecs);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("resource:ecs:delete")
    public R delete(@RequestBody Long[] ids){
		ecsService.removeEcsByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 获取主机和各个指标的关联信息
     * @param params
     * @return
     */
    @RequestMapping("/metric/list")
    public R metricList(@RequestParam Map<String, Object> params){
        PageUtils page = ecsService.queryMetricVoPageByType(params);

        return R.ok().put("page",page);
    }

    /**
     * 添加主机和各个指标的关联信息
     * @param paramString
     * @return
     */
    @PostMapping("/metric/add")
    public R metricAdd(@RequestBody String paramString){
        JSONObject jsonObject = JSONObject.parseObject(paramString);
        Long hostId = jsonObject.getLong("hostId");
        JSONArray jsonArray = jsonObject.getJSONArray("metricIds");
        List<Long> metricIds = jsonArray.toJavaList(Long.class);
        ecsService.addMetrics(hostId,metricIds);

        return R.ok();
    }

    /**
     * 删除主机和各个指标的关联信息
     * @param paramString
     * @return
     */
    @PostMapping("/metric/delete")
    public R metricDelete(@RequestBody String paramString){
        JSONObject jsonObject = JSONObject.parseObject(paramString);
        Long hostId = jsonObject.getLong("hostId");
        JSONArray jsonArray = jsonObject.getJSONArray("metricIds");
        List<Long> metricIds = jsonArray.toJavaList(Long.class);
        ecsService.cancelMetrics(hostId,metricIds);

        return R.ok();
    }



}
